<?php
declare(strict_types=1);
/**
 * @project: 兔兔找图微信小程序
 * @author: kert
 * @date: 2023/5/16
 * @link: https://www.tutudati.com/
 * @site: 微信搜索-兔兔找图
 */

namespace App\Admin\Controllers\Ai;

use App\Library\SnowFlakeId;
use App\Models\Admin\Ai\AiApp;
use App\Models\Admin\Ai\AiGroup;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class AiAppController extends AdminController
{
    protected $title = "应用管理";

    public function grid(): Grid
    {
        $grid = new Grid(new AiApp());
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->like('title', "应用名称");
                $filter->equal('is_show', "上架状态")->select([
                    1 => '上架',
                    2 => '下架',
                ]);
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('group_uid', "资源资源")->select(AiGroup::getList());
                $filter->between('created_at', "创建时间")->datetime();
            });
        });
        $grid->paginate(20);
        $grid->model()->orderByDesc("id");

        $grid->column("uid", "数据编号")->copyable();
        $grid->column("title", "应用名称");
        $grid->column("cover", "应用图标")->lightbox(['width' => 50, 'height' => 50]);
        $grid->column("orders", "显示顺序");
        $grid->column("group.title", "应用分组");
        $grid->column("page_url", "应用地址");
        $grid->column('is_show', "上架状态")->display(function ($is_show) {
            if ($is_show == 1) {
                return "<span style='color:#059B50'>上架</span>";
            }
            return "<span style='color:#E70F0FFF'>下架</span>";
        });
        $grid->column('bg_color', "上架状态")->display(function ($bg_color) {
            return "<span style='color:$bg_color;'>卡片背景色</span>";
        });
        $grid->column("created_at", "创建时间");
        $grid->disableExport();
        $grid->actions(function ($actions) {
            $actions->disableView();
        });
        return $grid;
    }

    public function form(): Form
    {
        $form = new Form(new AiApp());
        $form->select("group_uid", "资源分类")->options(AiGroup::getList());
        $form->hidden("uid", "应用编号")->default(SnowFlakeId::getId());
        $form->text("title", "应用名称")->rules('required|max:20');
        $form->text("page_url", "应用地址")->required()->help("填写用户端对应的页面地址");
        $form->number("orders", "显示顺序")->default(0)->help("值越大，显示权重越高，最大值99999999");
        $form->radio("is_show", "上架状态")->options([1 => "上架", 2 => "下架"])->default(1);
        $form->text("bg_color", "背景色")->default("#209CFF")->help("用户端卡片显示背景色，填写十六进制格式。");
        $form->image("path", "应用图标")->uniqueName()->required();
        $form->hidden("url", "应用图标")->default(env("QINIU_URL"));

        return $form;
    }
}
